<?php

namespace App\Http\Controllers\Batch;

use Illuminate\Http\Request;

use Session;
use Config;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Service\Batch\BatchService;

class BatchController extends Controller
{

    /**
     * @param Request $request
     * 账号批量续费
     */
    public function batchRenewUserAccount(Request $request)
    {

        $where = [];
        $where = $request->except('_token');
        $where['token'] = Session::get(config::get('custom.setting.MEMBER_TOKEN'));
        $where['batch_username'] = trim($where['batch_username'],',');


        $batch = new BatchService();
        $data = $batch->BatchRenewAccount($where);
        return $data;

    }


    /**
     * @param Request $request
     * @return bool|mixed|string
     * 批量添加用户
     */
    public function batchAddUserAccount(Request $request)
    {
        $where = [];
        $where = $request->except('_token');
        $where['token'] = Session::get(config::get('custom.setting.MEMBER_TOKEN'));


//        if (preg_match("/[\x7f-\xff]/", $where['batch_add_username']))
//        {
//            echo "<script>confirm('非法的用户名'); history.back() ;</script>";
//            exit();
//        }
//
//        if (preg_match("/[\x7f-\xff]/", $where['batch_add_password']))
//        {
//            echo "<script>confirm('非法的密码'); history.back() ;</script>";
//            exit();
//        }

        if($where['batch_add_num'] <= 0)
        {
            return ['error_code'=>-1,'error_message'=>'账户数量有误，请重新填写'];
        }



        preg_match_all('/\d+/',$where['batch_add_username'],$arr);

        $num = 0;

        if(!empty($arr[0]))
        {
            $num = $arr[0];

            $where['batch_add_username'] = strstr($where['batch_add_username'],"$num[0]",true);
        }
        $name = [];

        for($i=1;$i<$where['batch_add_num']+1;$i++)
        {
            if($num[0])
            {

                $number =  $i+$num[0];
                $name[] .= $where['batch_add_username'].$number;

            }else{
                $name[] .= $where['batch_add_username'].$i;
            }
        }

        if(count($name) >= 2)
        {
            $where['username'] = current($name).'-'.end($name) ;
        }else{
            $where['username'] = current($name);
        }

        $where['batch_add_username'] = implode($name,',');



        $batch = new BatchService();
        $data = $batch->BatchAddAccount($where);

        return $data;

    }

    


}
